layui.define(['table', 'treeTable', 'xmSelect', 'tableSelect', 'form', 'laydate'], function (exports) {

    var $ = layui.$
        , form = layui.form
        , table = layui.table
        , setter = layui.setter
        , admin = layui.admin
        , treeTable = layui.treeTable
        , tableSelect = layui.tableSelect
        , laydate = layui.laydate
        , xmSelect = layui.xmSelect

    var templatecode;
    var templatename;
    var parentJson = {};
 	var jobnumber = sessionStorage.getItem('jobnumber') == null?"":sessionStorage.getItem('jobnumber');
//	var laydate = layui.laydate;
	var time = new Date();
    var day = ("0" + time.getDate()).slice(-2);
    var month = ("0" + (time.getMonth() + 1)).slice(-2);
    var today = time.getFullYear() + "-" + (month) + "-" + (day) + " - "+time.getFullYear() + "-" + (month) + "-" + (day);
    var theday = time.getFullYear() + "-" + (month) + "-" + (day);
    var theshopcode ;
    var device = layui.device();
    if(device.os == 'windows'){//判断是电脑
    	$('style[mainStyle]').append('div,td,input,span,button,a,select,ul li,i,cite{font-size:14px!important;}');
	}else{//判断是平板
		if(window.screen.width > 1366){
			$('style[mainStyle]').append('div,td,input,span,button,a,select,ul li,i,cite{font-size:18px!important;}');
		}else{
			$('style[mainStyle]').append('div,td,input,span,button,a,select,ul li,i,cite{font-size:14px!important;}');
		}
	}
	var goodscode = document.getElementById("wheregdscode");
	goodscode.addEventListener("keyup", function(event) {
	    event.preventDefault();
	    if (event.keyCode === 13) {
			$('button[lay-filter="LAY-genius-normal-edit"]').trigger('click');
	    }
	});
	  //执行一个laydate实例
	  laydate.render({
	    elem: '#whereDate' //指定元素
	   ,range: true 
	   ,value: today
	   ,btns: ['confirm']
	  });

    var obj = {
        setParentJson: function setParentJson(json) {
           // parentJson = json;
            if(jobnumber != '' && jobnumber.length == 4){
            	$('input[name="whereshopcode"]').val(jobnumber);
            	$('input[name="whereshopcode"]').attr('readonly','readonly')
            }
            loadTime()
        }

    }

//	------------------------------------------------------------------------------------------- 	
	var operationTpl = function(d){return '<a lay-event="operationTpl" style="cursor: pointer" ><span  style="color:#1E9FFF">'+'删除'+'</span></a>';};
    function loadTime() {
        colsArr = [
            {field: 'Gdsincode', fixed: 'left', title: '商品编码', align: 'center'},
            {field: 'Gdsname', title: '商品名称', align: 'center'},
            {field: 'State', title: '状态', align: 'center'},
            {field: 'Sqdate', title: '申请时间', align: 'center'},
 			{field: '', title: '操作', align: 'center',templet: operationTpl},
        ]
        parentJson.Deptcode = "";
        parentJson.Gdscode = "";
        parentJson.BeginDate= theday; 
        parentJson.EndDate = theday;
        //商品
        table.render({
            elem: '#goods-table'
          //  , url: setter.host + '/yaohuo/jiaqiansq'
            , cols: [colsArr]
            , method: 'post'
            , contentType: 'application/json; charset=utf-8'
            //, where: parentJson
			,data:[]
        	,totalRow: true
           // , page: true
            , limit: 20
            , limits: [30, 50, 70]
            , height: 'full-80'
            , text: {
                none: '暂无数据'
            }
            ,done:function(res, curr, count){ 
				//console.log(res);
			}
        });
    	if($('input[name="whereshopcode"]').val() != ''){
        	$('button[lay-filter="LAY-genius-normal-search"]').trigger('click');
        }
    }

//	------------------------------------------------------------------------------------------- 	
    $('.layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });


    var active = {
        add: function () {
            dateType = 'add';
        }
    }
//------------------------------------------------------
    $("#back").on("click", function () {
        bakUrl = setter.host + '/buhuo_param_shop/toSelect'
        window.location.href = bakUrl;
    })
	table.on('tool(goods-table)', function(obj){
		var data = obj.data //获得当前行数据
	    ,layEvent = obj.event; //获得 lay-event 对应的值

		if(layEvent === 'operationTpl'){ 
			var delData = {};
			delData.Deptcode =theshopcode;
			delData.Gdscode = data.Gdsincode;
			delData.Type  = data.Type;
			console.log(delData);
    		layui.admin.req({
    			url: setter.host + '/yaohuo/jiaqiansqDelete'
    			, type: 'post'
				, dataType: 'json'
				, contentType: "application/json; charset=utf-8"
				, data: JSON.stringify(delData)
				, done: function (res) {
					
					//请求成功后
					if (res.status == '1') {
						table.reload('goods-table');
						layer.msg(res.msg);
					} else {
						layer.msg(res.msg);
					}
				}
    		});
		}
	});
//事件
    var active = {
        save: function () {
            var sendData = {};
            var whereshopcode =  document.getElementById("whereshopcode").value;
            var wheregdscode  =  document.getElementById("wheregdscode").value;
 			var wherestatus  =  document.getElementById("wherestatus").value;
			if(whereshopcode != ''){
		  		sendData.Deptcode = whereshopcode;
			}else{
				return layer.msg('门店编码不能为空');
			}
            if(wheregdscode != ''){
			 	sendData.Gdscode= wheregdscode;
		    }else{
				return layer.msg('商品码不能为空');
		    }
  			sendData.Group = '';
	        sendData.Type = wherestatus;
			if(sendData.Type=='2'){
				sendData.Group = sendData.Gdscode;
				sendData.Gdscode = '';
			}
            console.log(sendData);
	
    		var loading = layer.msg('正在申请中', {icon: 16, shade: 0.1, time:0});
    		layui.admin.req({
    			url: setter.host + '/yaohuo/jiaqiansqSave'
    			, type: 'post'
				, dataType: 'json'
				, contentType: "application/json; charset=utf-8"
				, data: JSON.stringify(sendData)
				, done: function (res) {
					layer.close(loading);
					$("#wheregdscode").val("");
					//请求成功后
					if (res.status == '1') {
						
						table.reload('goods-table');
						layer.msg(res.msg);
					} else {
						layer.msg(res.msg);
					}
				}
    		});
         
          
        }
    };


	//监听搜索
	form.on('submit(LAY-genius-normal-search)', function(data){
		if(data.field.whereshopcode != ''){
		  	parentJson.Deptcode = data.field.whereshopcode;
			theshopcode = data.field.whereshopcode;
		}else{
			return layer.msg('门店编码不能为空');
		}	
		if(data.field.wheregdscode != ''){
			 parentJson.Gdscode= data.field.wheregdscode;
		}else{
			parentJson.Gdscode = "";
		}
		if(data.field.whereDate != ''){
			parentJson.BeginDate = data.field.whereDate.substring(0,10);
			parentJson.EndDate = data.field.whereDate.substring(13,23);	
		}	
		console.log(parentJson);
		//执行重载
		table.reload('goods-table', {
			where: parentJson
			, url: setter.host + '/zsyaohuo/jiaqiansq'
		});
	});
	
	
    exports('insert', obj);
});
//---------------------------------------------------------------------------
//roleTable = table.render({
//elem: '#LAY-role-list1'
//,url:'/unite_platform/goods_class/getclasslabel'
//,cols: [[
//{type:'checkbox'}
//,{field:'id', width:80, title: 'ID', sort: true}
//,{field:'name', width:80, title: '用户名'}
//]]
//,page: true
//});



